//
//  KQChatBaseViewController.swift
//  KQChatKit
//
//  Created by kangqiao on 2021/9/15.
//

import UIKit
import KQHBDNavigationBar
/// 导航条颜色
public enum KQBaseNavigationBarColor: Int {
    case defualt = 0 // 默认
    case orange = 1 // 橘色
    case clear = 2 // 橘色

}

/// 导航栏左右Item
public enum KQBaseNavigationItemType: Int {
    case left = 0 // 左
    case right = 1 // 右
}

open class KQChatBaseViewController: UIViewController {
    // MARK: - life cycle

    open override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        if #available(iOS 11.0, *) {
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        // 默认不隐藏导航栏
        hbd_barHidden = false
        // 创建导航栏左侧按钮
        initBarItem(customView: leftNavigationButton, type: .left)
        // 创建导航栏右侧按钮
        initBarItem(customView: rightNavigationButton, type: .right)
        // 设置导航栏样式
        setNavigationBar(barColor: .defualt, barShadowHidden: true)
        // view背景色
        view.backgroundColor = KQCHAT_COLOR(242, 242, 242)
        // 默认右侧导航栏按钮隐藏
        rightNavigationButton.isHidden = true
        // 默认可以手势返回
        hbd_backInteractive = true
        
    }

    deinit {
        print("---------\(type(of: self))---dealloc---------")
    }

    // MARK: - UI

    open override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }

    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }

    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }

    // MARK: - methods

    /// 状态栏是否隐藏
    open override var prefersStatusBarHidden: Bool {
        return false
    }

    /// 状态栏样式
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }

    /// 创建UIBarButtonItem
    /// - Parameters:
    ///   - customView: 自定义View
    ///   - type: 类型：左侧、右侧
    open func initBarItem(customView: UIView, type: KQBaseNavigationItemType) {
        if type == .left {
            let leftItem = UIBarButtonItem(customView: customView)
            let leftItems = [leftItem]
            navigationItem.leftBarButtonItems = leftItems
        } else if type == .right {
            let rightItem = UIBarButtonItem(customView: customView)
            let rightItems = [rightItem]
            navigationItem.rightBarButtonItems = rightItems
        }
    }

    /// NavigationBar样式
    /// - Parameters:
    ///   - barColor: bar的颜色
    open func setNavigationBar(barColor: KQBaseNavigationBarColor, barShadowHidden: Bool) {
        // 是否隐藏阴影
        hbd_barShadowHidden = barShadowHidden

        // 背景色
        if barColor == .defualt {
            
            self.hbd_barTintColor = .white
            self.hbd_barImage = UIImage(named: "kq_navigation_bar_bg_white.png")
            self.hbd_titleTextAttributes = [NSAttributedString.Key.foregroundColor: KQCHAT_COLOR(0, 0, 0),
                                       NSAttributedString.Key.font: UIFont(name: KQCHAT_FONT_MEDIUM_NAME, size: 17)!]
            leftNavigationButton.setImage(UIImage(named: "kq_btn_back.png"), for: .normal)
        } else if barColor == .orange {
            
            self.hbd_barTintColor = .white
            self.hbd_barImage = UIImage(named: "kq_navigation_bar_bg_white.png")
            self.hbd_titleTextAttributes = [NSAttributedString.Key.foregroundColor: KQCHAT_COLOR(0, 0, 0),
                                       NSAttributedString.Key.font: UIFont(name: KQCHAT_FONT_MEDIUM_NAME, size: 17)!]
            leftNavigationButton.setImage(UIImage(named: "kq_btn_back.png"), for: .normal)
        }
    }

    // MARK: - events

    /// 导航栏左侧按钮点击
    @objc open func leftNavigationButtonClick(_: Any?) {
        navigationController?.popViewController(animated: true)
    }

    /// 导航栏右侧按钮点击
    @objc open func rightNavigationButtonClick(_: Any?) {
//        self.navigationController?.popToRootViewController(animated: true)
    }

    // MARK: - getters & setters

    /// 导航栏左侧按钮
    open lazy var leftNavigationButton: UIButton = {
        let leftButton = UIButton(type: .custom)
        leftButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        leftButton.imageEdgeInsets = UIEdgeInsets(top: 13, left: 17, bottom: 13, right: 17)
        leftButton.isExclusiveTouch = true
        leftButton.addTarget(self, action: #selector(leftNavigationButtonClick(_:)), for: .touchUpInside)
        return leftButton
    }()

    /// 导航栏右侧按钮
    open lazy var rightNavigationButton: UIButton = {
        let rightButton = UIButton(type: .custom)
        rightButton.frame = CGRect(x: 0, y: 0, width: 54, height: 44)
        rightButton.titleLabel?.font = UIFont(name: KQCHAT_FONT_NAME, size: 15)
        rightButton.isExclusiveTouch = true
        rightButton.addTarget(self, action: #selector(rightNavigationButtonClick(_:)), for: .touchUpInside)
        return rightButton
    }()
}

extension UIViewController {
    
    /// 去登录
    public func goLoginVC() {
//        if KQUserManager.shared().isLogin() == true {
//            assert(false, "请使用loginOut方法")
//        }
//
//        // 进入登录页面
//        let loginVC = KQLoginViewController()
//        let navi = HBDNavigationController(rootViewController: loginVC)
//        navi.modalPresentationStyle = .fullScreen
//        self.kq_currentController()?.present(navi, animated: true, completion: nil)
    }

    
    /// 退出登录
    /// - Parameter backToFirstVC: 是否返回到第一个页面，默认true；removeLastUserPhone：是否清除上次登录手机号 默认true
    public func loginOut(backToFirstVC: Bool = true, removeLastUserPhone: Bool = true) {
//        if KQUserManager.shared().isLogin() == true {
//            // 告知接口退出、并退出IM
//            _ = KQNetworkRequest.shared().loginOut(success: nil, failure: nil)
//            // 清除本地user
//            KQUserManager.shared().remove()
//            if removeLastUserPhone == true {
//                // 清除上次登录的用户id
//                KQUserManager.shared().removeLastUserId()
//            }
//            // 清除user的手机号
//            KQCacheManager.shared().remove(String.self, extendKey: "KQ_USER_PHONE")
//            // 退出通知
//            NotificationCenter.default.post(name: NSNotification.Name(rawValue: KQ_LOGIN_OUT_NOTIFICATION), object: nil)
//        }
//        
//        if backToFirstVC == true {
//            // 返回到我的页面
//            self.navigationController?.popToRootViewController(animated: false)
//        }
//        
//        // 进入登录页面
//        let loginVC = KQLoginViewController()
//        let navi = HBDNavigationController(rootViewController: loginVC)
//        navi.modalPresentationStyle = .fullScreen
//        self.kq_currentController()?.present(navi, animated: true, completion: nil)
    }

    /// 从登录页面返回到Tabbar的子页面
    public func dismissLoginVC(completion: (() -> Void)?) {
        // dismiss
        self.navigationController?.dismiss(animated: true, completion: {
            completion?()
        })
    }
    
}
